package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 订单信息api控制器
 *
 * @author ym
 * @date 2025/06/28 21:58
 */
@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

	@Autowired
	private OrderInfoService orderInfoService;

	/**
	 * 贸易
	 *
	 * @return {@link Result<>}
	 */
	@GuiGuLogin
	@PostMapping("/orderInfo/trade")
	public Result<OrderInfoVo> trade(@RequestBody TradeVo tradeVo) {
		Long userId = AuthContextHolder.getUserId();
		OrderInfoVo orderInfoVo = orderInfoService.trade(userId, tradeVo);
		return Result.ok(orderInfoVo);
	}

	@GuiGuLogin
	@PostMapping("/orderInfo/submitOrder")

	public Result<Map<String, Object>> submitOrder(@RequestBody OrderInfoVo orderInfoVo) {
		Long userId = AuthContextHolder.getUserId();
		Map<String, Object> map = orderInfoService.submitOrder(userId, orderInfoVo);
		return Result.ok(map);

	}
	@GuiGuLogin
	@GetMapping("/orderInfo/getOrderInfo/{orderNo}")
	public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
		Long userId = AuthContextHolder.getUserId();
		OrderInfo orderInfo = orderInfoService.getOrderInfo(userId, orderNo);
		return Result.ok(orderInfo);
	}

	/**
	 * 用户订单
	 *
	 * @param pageNum 页码
	 * @param limit   极限
	 * @return {@link Result}
	 */
	@GuiGuLogin
	@GetMapping("/orderInfo/findUserPage/{pageNum}/{limit}")
	public Result<Page<OrderInfo>> findUserPage(@PathVariable Integer pageNum, @PathVariable Integer limit) {
		Long userId = AuthContextHolder.getUserId();
		Page<OrderInfo> orderInfoPage = orderInfoService.findUserPage(userId, pageNum, limit);
		return Result.ok(orderInfoPage);
	}
}

